package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.ClockInDao;
import cn.topevery.garden.entity.fetch.ClockFetch;
import cn.topevery.garden.entity.po.ClockIn;
import cn.topevery.garden.entity.query.ClockInQuery;
import cn.topevery.garden.entity.vo.ClockInVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author code-generate
 * @date 2022-01-05
 */
@Repository
public class ClockInDaoImpl extends BaseDaoImpl<ClockIn> implements ClockInDao {
    @Override
    public <P extends DbCsp> List<ClockIn> getSearch(P para) {
        return null;
    }


    @Override
    public DataPager<ClockInVO> listPage(ClockInQuery query) {
        String table = "t_clock_in tci";

        String columns = "tci.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tci.c_id desc");
        sqlQueryMate
                .where(true, "tci.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getName()), "tci.c_name", ScriptConditionEnum.Like, query.getName())
                .where(ObjectUtils.isNotEmpty(query.getBeginDate()), "tci.c_date", ScriptConditionEnum.GreaterThanEq, query.getBeginDate())
                .where(ObjectUtils.isNotEmpty(query.getEndDate()), "tci.c_date", ScriptConditionEnum.LessThanEq, query.getEndDate())
                .where(query.getIsUse() != null, "tci.c_is_use", ScriptConditionEnum.Eq, query.getIsUse());

        return getDataPager(sqlQueryMate, pageSize, pageIndex, ClockInVO.class);
    }

    @Override
    public ClockIn getEntityByColumn(String key, String value) {
        KeyValue<String> map = new KeyValue<>();
        map.key = key;
        map.value = value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public DataPager<ClockIn> getList(ClockFetch para) {
        String table = "t_clock_in tci";

        String columns = "tci.*";

        int pageIndex = para.getPageIndex() - 1;
        int pageSize = para.getPageSize();

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tci.c_id desc");
        sqlQueryMate
                .where(true, "tci.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(para.getCompanyName() != null, "tci.c_dept_name", ScriptConditionEnum.Eq, para.getCompanyName())
                .where(para.getMinDate() != null, "tci.c_date", ScriptConditionEnum.GreaterThanEq, para.getMinDate())
                .where(para.getType() != null, "tci.c_type", ScriptConditionEnum.Eq, para.getType());

        return getDataPager(sqlQueryMate, pageSize, pageIndex, ClockIn.class);
    }

    @Override
    public List<ClockIn> getClockInList(ClockInQuery query) {
        StringBuffer sb = new StringBuffer();
        sb.append("select * from t_clock_in where c_db_status = 0 and c_db_create_id" + query.userId);
        sb.append(" and c_db_create_date >=" + query.getBeginDate());
        sb.append(" and c_db_create_date <=" + query.getEndDate());
        AtomicReference<List<ClockIn>> vo = new AtomicReference<>(new ArrayList<>());
        run(sb.toString(), CommandType.Text, dbCommand -> {
            vo.set(dbCommand.executeToList(ClockIn.class));
        });

        return vo.get();
    }

    @Override
    public void updateClockRecord(String userId) {
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_clock_in")
                .setColumnValue(true, "c_is_use", 0)
                .setColumnValue(true, "c_db_last_update_id", userId)
                .setColumnValue(true, "c_db_last_update_date", LocalDateTime.now());
        sqlUpdateMate
                .where(true, "c_date", ScriptConditionEnum.GreaterThanEq, LocalDate.now())
                .where(true, "c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "c_type", ScriptConditionEnum.Eq, 2);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }
}
